package com.rayc.test;

import com.rayc.entity.User;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
import redis.clients.jedis.Jedis;

import java.util.Date;

public class demo2 {

    /**
     * 存储对象  byte[]形式
     */
    @Test
    public void setByteArray(){
        //1.连接redis
        Jedis jedis = new Jedis("192.168.232.128",6379);
        //2.准备key(String)-value(User)
        String key = "user";
        User value = new User(1,"张三",new Date());
        //3 将key-value转成byte[]
        byte[] byteKey = SerializationUtils.serialize(key);
        byte[] byteValue = SerializationUtils.serialize(value);
        //要么全String 要么全byte[]
        jedis.set(byteKey, byteValue);
        //4.释放资源
        jedis.close();
    }

    /**
     * 获取对象  byte[]形式
     */
    @Test
    public void getByteArray(){
        //1.连接redis
        Jedis jedis = new Jedis("192.168.232.128",6379);
        //2.准备key(String)-value(User)
        String key = "user";
        //3 将key-value转成byte[]
        byte[] byteKey = SerializationUtils.serialize(key);
        //要么全String 要么全byte[]
        byte[] byteValue = jedis.get(byteKey);
        User user = (User)SerializationUtils.deserialize(byteValue);
        System.out.println(user.getName());
        //4.释放资源
        jedis.close();
    }
}
